'use strict';

const Controller = require('egg').Controller;

class UserController extends Controller {
  async index() {
    const {ctx} = this;
    // ctx.body = {code:200};
    let option = ctx.helper.formatOptions(ctx.query,["userName","phonenumber","status","deptId"]);
    option.include = [ctx.model.SysDept];
    try {
      let result = await ctx.model.SysUser.findAndCountAll(option);
      result.rows = result.rows.map(el=>{
        let obj = el.toJSON();
        obj.dept = obj.sys_dept;
        delete obj.sys_dept;
        return obj;
      })
      ctx.body = ctx.helper.formateResponseList(result);
    } catch (error) {
      ctx.body = ctx.helper.formateResponse(null,"查询失败",310)
    }
  }
  async create(){
    const {ctx,app} = this;
    const {Op} = app.Sequelize;
    const data = ctx.request.body;
    let option = {
      deptId:data.deptId,
      email:data.email,
      nickName:data.nickName,
      password:data.password,
      phonenumber:data.phonenumber,
      remark:data.remark,
      sex:data.sex,
      status:data.status,
      userName:data.userName
    };

    const user = await ctx.model.SysUser.create(option);
    let posts = [];
    if(data.postIds.length){
      posts = await ctx.model.SysPost.findAll({
        where:{
          postId:{
            [Op.or]:data.postIds
          }
        }
      })
    }
    await user.setSys_posts(posts);
    let roles = [];
    if(data.roleIds.length){
      roles = await ctx.model.SysRole.findAll({where:{roleId:{[Op.or]:data.roleIds}}});
    }
    await user.setSys_roles(roles);

    ctx.body = ctx.helper.formateResponse(user);
  }
  async show(){
    const {ctx} = this;
    const {id} = ctx.params;
    let user = await ctx.model.SysUser.findOne({
      where:{
        userId:id
      },
      include:[ctx.model.SysPost,ctx.model.SysRole,ctx.model.SysDept]
    });

    let postIds = user.sys_posts.map(el=>el.postId);
    let roleIds = user.sys_roles.map(el=>el.roleId);

    let posts = await ctx.model.SysPost.findAll();
    let roles = await ctx.model.SysRole.findAll();

    user.dept = user.sys_dept;
    delete user.sys_posts;
    delete user.sys_roles;
    delete user.sys_dept;

    let responseData = {
      code:200,
      msg:"操作成功",
      data:user,
      postIds,
      posts,
      roleIds,
      roles
    }

    ctx.body = responseData;

  }
  async restePsd(){
    const {ctx} = this;
    const data = ctx.request.body;
    const user = await ctx.model.SysUser.findByPk(Number(data.userId));
    await user.update(
      {password:data.password}
    )
    ctx.body = ctx.helper.formateResponse(null);
  }
  async destroy(){
    const {ctx} = this;
    const {id} = ctx.params;
    const user = await ctx.model.SysUser.findByPk(Number(id));
    await user.setSys_posts([]);
    await user.setSys_roles([]);
    await user.destroy();
    ctx.body = ctx.helper.formateResponse(user);
  }
  async update(){
    const {ctx,app} = this;
    const {Op} = app.Sequelize;
    const data = ctx.request.body;
    let option = {
      deptId:data.deptId,
      email:data.email,
      nickName:data.nickName,
      password:data.password,
      phonenumber:data.phonenumber,
      remark:data.remark,
      sex:data.sex,
      status:data.status,
      userName:data.userName
    };

    const user = await ctx.model.SysUser.findByPk(data.userId);
    await user.update(option);
    let posts = [];
    if(data.postIds.length){
      posts = await ctx.model.SysPost.findAll({
        where:{
          postId:{
            [Op.or]:data.postIds
          }
        }
      })
    }
    await user.setSys_posts(posts);
    let roles = [];
    if(data.roleIds.length){
      roles = await ctx.model.SysRole.findAll({where:{roleId:{[Op.or]:data.roleIds}}});
    }
    await user.setSys_roles(roles);

    ctx.body = ctx.helper.formateResponse(user);
  }
  async changeStatus(){
    const {ctx} = this;
    const data = ctx.request.body;
    const user = await ctx.model.SysUser.findByPk(data.userId);
    await user.update({
      status:data.status
    });
    ctx.body = ctx.helper.formateResponse(null);
  }

  async getOption(){
    const {ctx} = this;
    const roles = await ctx.model.SysRole.findAll();
    const posts = await ctx.model.SysPost.findAll();
    ctx.body = {
      code:200,
      msg:"查询成功",
      roles,
      posts
    }
  }
}

module.exports = UserController;
